package cn.t.converter.device;


import cn.t.constants.CommonConstants;
import cn.t.core.converter.CoreDTOConverter;
import cn.t.dto.device.CargowayDTO;
import cn.t.dto.order.ExcelBatchOnShelfDTO;
import cn.t.model.device.CargowayDO;
import cn.t.model.device.CargowaySkuDO;
import cn.t.model.sku.SkuDO;
import cn.t.vo.device.CargowaySkuVO;
import cn.t.vo.device.DeviceDetailVO;
import org.apache.commons.lang3.ObjectUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.math.BigDecimal;

/**
 * 货道表(Cargoway)dto转domain
 *
 * @author t
 * @since 2024-05-24 10:19:33
 */
@Mapper(componentModel = "spring")
public interface CargowayDTOConverter extends CoreDTOConverter<CargowayDTO, CargowayDO> {

    @Mapping(target = "skuName", source = "cargowaySku.skuName")
    @Mapping(target = "deviceId", source = "cargoway.deviceId")
    @Mapping(target = "skuCode", source = "cargowaySku.skuCode")
    @Mapping(target = "weight", source = "cargowaySku.skuWeight")
    @Mapping(target = "cargowayId", source = "cargoway.id")
    @Mapping(target = "cargowayWeight", source = "cargowaySku.weight")
    @Mapping(target = "skuId", source = "cargowaySku.skuId")
    @Mapping(target = "cargowaySkuId", source = "cargowaySku.id")
    @Mapping(target = "inventoryQty", source = "cargowaySku.inventory")
    @Mapping(target = "cargowayNo", source = "cargoway.cargowayNo")
    @Mapping(target = "realInventoryQty", expression = "java(cn.t.utils.BusinessUtil.calcQty(cargowaySku))")
    DeviceDetailVO.CargowayDetail data2CargowayDetailVo(CargowayDO cargoway, CargowaySkuDO cargowaySku);

    @Mapping(target = "skuName", source = "cargowaySku.skuName")
    @Mapping(target = "deviceId", source = "cargoway.deviceId")
    @Mapping(target = "skuCode", source = "cargowaySku.skuCode")
    @Mapping(target = "weight", source = "cargowaySku.skuWeight")
    @Mapping(target = "cargowayId", source = "cargoway.id")
    @Mapping(target = "cargowayWeight", source = "cargowaySku.weight")
    @Mapping(target = "skuId", source = "cargowaySku.skuId")
    @Mapping(target = "cargowaySkuId", source = "cargowaySku.id")
    @Mapping(target = "inventoryQty", source = "cargowaySku.inventory")
    @Mapping(target = "cargowayNo", source = "cargoway.cargowayNo")
    @Mapping(target = "realInventoryQty", expression = "java(cn.t.utils.BusinessUtil.calcQty(cargowaySku))")
    CargowaySkuVO data2CargowaySkuVo(CargowayDO cargoway, CargowaySkuDO cargowaySku);

    default CargowaySkuDO createUpdateCargowayGoods(ExcelBatchOnShelfDTO batchOnShelfDTO, CargowaySkuDO cargowaySku, CargowayDO cargoway, SkuDO skuDO) {
        Integer cargowayVol;
        Integer alarmLimit;
        Integer inventory;
        //上架
        if (ObjectUtils.isEmpty(cargowaySku)) {
            cargowaySku = new CargowaySkuDO();
            cargowayVol = ObjectUtils.isEmpty(batchOnShelfDTO.getCargowayVol()) ? CommonConstants.ZERO : batchOnShelfDTO.getCargowayVol();
            alarmLimit = ObjectUtils.isEmpty(batchOnShelfDTO.getAlarmLimit()) ? CommonConstants.ZERO : batchOnShelfDTO.getAlarmLimit();
            inventory = ObjectUtils.isEmpty(batchOnShelfDTO.getInventory()) ? CommonConstants.ZERO : batchOnShelfDTO.getInventory();
            cargowaySku.setWeight(new BigDecimal("0.00"));
        }
        //重复上架
        else {
            cargowayVol = ObjectUtils.isEmpty(batchOnShelfDTO.getCargowayVol()) ? cargoway.getCargowayVol() : batchOnShelfDTO.getCargowayVol();
            alarmLimit = ObjectUtils.isEmpty(batchOnShelfDTO.getAlarmLimit()) ? cargowaySku.getAlarmLimit() : batchOnShelfDTO.getAlarmLimit();
            inventory = ObjectUtils.isEmpty(batchOnShelfDTO.getInventory()) ? cargowaySku.getInventory().intValue() : batchOnShelfDTO.getInventory();
        }

        cargowaySku.setSkuCode(skuDO.getSkuCode());
        cargowaySku.setSkuName(skuDO.getSkuName());
        cargowaySku.setSkuImg(skuDO.getSkuImg());
        cargowaySku.setSkuWeight(skuDO.getWeight());
        cargowaySku.setErrorRate(skuDO.getErrorRate());
        cargowaySku.setSkuId(skuDO.getId());
        cargowaySku.setAlarmLimit(alarmLimit);
        cargowaySku.setInventory(inventory);
        cargowaySku.setCargowayId(cargoway.getId());
        cargowaySku.setDeviceId(cargoway.getDeviceId());
        cargowaySku.setSize(skuDO.getSize());
        cargowaySku.setCargowayNo(cargoway.getCargowayNo());

        cargoway.setCargowayVol(cargowayVol);
        return cargowaySku;
    }
}

